<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="短信验证码的内容加强">
<meta property="og:type" content="article">
<meta property="og:title" content="一次项目短信验证码整改实验">
<meta property="og:url" content="https://whitestore.top/2020/10/29/mail/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="短信验证码的内容加强">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-10-29T13:20:42.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.256Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="短信验证码">
<meta property="article:tag" content="短信">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://whitestore.top/2020/10/29/mail/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>一次项目短信验证码整改实验 | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2020/10/29/mail/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          一次项目短信验证码整改实验
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-10-29 21:20:42" itemprop="dateCreated datePublished" datetime="2020-10-29T21:20:42+08:00">2020-10-29</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/10/29/mail/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/10/29/mail/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>18k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>16 分钟</span>
            </span>
            <div class="post-description">短信验证码的内容加强</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="一次短信验证码整改实验"><a href="#一次短信验证码整改实验" class="headerlink" title="一次短信验证码整改实验"></a>一次短信验证码整改实验</h1><h2 id="前言："><a href="#前言：" class="headerlink" title="前言："></a>前言：</h2><p>讨论内容部分为当初的一些短信验证码的需求细节讨论</p>
<p>这个短信验证码在并发量非常大的情况下有可能会失效，后续会进行整改升级，保证线程安全</p>
<a id="more"></a>

<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><p>短信验证码（要想着怎么把所有的项目都整改起来，不影响原有业务运行） 3天时间，全部替换掉</p>
<ul>
<li><p>发送短信</p>
<ul>
<li>增加【业务类型】</li>
<li>获取短信的时候，增加图片验证码（此处用第三方框架实现）</li>
<li>单独增加短信验证码的 ip访问控制 CheckSendSmsIpVisitHelper ，注意别和 CheckIpVisitHelper 冲突</li>
<li>校验手机号码长度 11 位 </li>
<li>60s 根据【手机号码+业务】判断只能发送一次短信，此处将 【手机号码+业务】作为 map 的 key 存储在上下文中</li>
<li>可以灵活配置【手机号码+业务】 每天能够获取短信的次数</li>
</ul>
</li>
<li><p>校验短信</p>
<ul>
<li>增加【手机号+短信验证码】的匹配</li>
<li>校验成功，清理掉session中存储的信息</li>
</ul>
</li>
</ul>
<h1 id="讨论内容"><a href="#讨论内容" class="headerlink" title="讨论内容"></a>讨论内容</h1><ul>
<li>验证码就用第三方的。<code>google kaptcha</code> 这个可以试试</li>
<li>这样 jydd apps 都可以用，2-3天可以完全替换掉所有业务的短信。</li>
<li>页面的修改，直接找建哥提供样式。</li>
<li>把所有业务都一并改了，省的隔三差五的出报告要整改</li>
<li>我要一个完全独立的短信验证码模块，虽然不能在按照项目模块来划分，那就完全独立出来包和功能以及依赖</li>
<li>其实可以直接在apps 的 core里面开发，然后那边直接引用调用？jar包的方式放进去就是的。</li>
</ul>
<h2 id="处理方案："><a href="#处理方案：" class="headerlink" title="处理方案："></a>处理方案：</h2><h2 id="第三方图形验证码参考"><a href="#第三方图形验证码参考" class="headerlink" title="第三方图形验证码参考"></a>第三方图形验证码参考</h2><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20veWFuZ3poaWxvbmcvcC84NTc0Njg1Lmh0bWw=" title="https://www.cnblogs.com/yangzhilong/p/8574685.html">SpringBoot之配置google kaptcha<i class="fa fa-external-link"></i></span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vRmx5SGVMYW5NYW4vcC82MjkzOTkxLmh0bWw=" title="https://www.cnblogs.com/FlyHeLanMan/p/6293991.html">SpringMvc项目中使用GoogleKaptcha 生成验证码<i class="fa fa-external-link"></i></span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly93d3cucmFtb3N0ZWFyLmNvbS9kb2MvaGFwcHktY2FwdGNoYQ==" title="https://www.ramostear.com/doc/happy-captcha">happy-captcha<i class="fa fa-external-link"></i></span></p>
<h3 id="图形验证码："><a href="#图形验证码：" class="headerlink" title="图形验证码："></a>图形验证码：</h3><ol>
<li>调整页面，增加图形验证码的模块（不同模块需要的改动点不一样）</li>
</ol>
<blockquote>
<p>也可以先把功能做出来，再让前端根据实际情况去调整样式</p>
</blockquote>
<ol>
<li>尝试增加 happy-captcha 或者 google-captcha （实际查询资料发现没有进行维护）</li>
<li>先不考虑美观问题，以实现功能为主要，后续需要改样式在找前端处理</li>
</ol>
<blockquote>
<p>20200903 已实现</p>
</blockquote>
<h3 id="短信校验"><a href="#短信校验" class="headerlink" title="短信校验"></a>短信校验</h3><ol>
<li>60s 根据【手机号码+业务】判断只能发送一次短信，此处将 【手机号码+业务】作为 map 的 key 存储在上下文中</li>
</ol>
<blockquote>
<ol>
<li>将短信的配置独立到一个单独的xml 文件中，方便 spirngboot 项目以及 spinrgmvc 管理</li>
<li>业务模块按照模板的格式配置，不跟项目走</li>
<li>xml 配置读取参考 节假日的xml 配置读取以及作用</li>
<li>整个部分可以参考节假日的处理方式，迁移比较方便</li>
</ol>
</blockquote>
<p>使用xml 配置手机+业务模块</p>
<ol start="2">
<li>校验手机号码长度 11 位 </li>
</ol>
<blockquote>
<ol>
<li>写到工具类里面，单独增加一个方法，需要的时候在调用的地方加入（方案一）</li>
<li>直接在调用的地方补充添加（方案二）</li>
</ol>
</blockquote>
<p>直接在短信接口加入即可</p>
<ol start="3">
<li>可以灵活配置【手机号码+业务】 每天能够获取短信的次数</li>
</ol>
<blockquote>
<ol>
<li>其实就是业务模块单个手机号码的限制次数</li>
</ol>
<p>PS: 目前的攻击手段可以用虚拟手机号码 + 肉鸡服务器 实现，手机号+业务的限制作用个人理解来看作用不明显</p>
</blockquote>
<ol start="4">
<li>大致的工具类设置</li>
</ol>
<ul>
<li>初始化读取xml配置</li>
<li>加载一些动态配置到属性里面，包括一些校验次数的限制</li>
<li>单例模式</li>
<li>尽量少的使用三方工具包，尽量使用原生java实现</li>
<li>注意jdk 版本的问题，不以jdk1.8 为基准</li>
<li>CheckSendSmsIpVisitHelper 可以是对 CheckIpVisitHelper  的扩展</li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cucmFtb3N0ZWFyLmNvbS9kb2MvaGFwcHktY2FwdGNoYQ==" title="https://www.ramostear.com/doc/happy-captcha">happy-captcha<i class="fa fa-external-link"></i></span> 以此作为参考实现 ，google 的图形化验证码比较老了</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ol>
<li>目前先尝试 使用一下是否可行， 如果可行在进行处理</li>
<li>先不考虑样式问题，先以实现功能保证可用并可以迁移为主</li>
<li>多测试，保证功能稳定，在考虑迁移到apps </li>
</ol>
<h3 id="大致流程"><a href="#大致流程" class="headerlink" title="大致流程"></a>大致流程</h3><ol>
<li>输入手机号码</li>
<li>在点击发送短信按钮之前，弹出输入图形验证码</li>
<li>输入正确的图形验证码，发送短信，图形验证取消，回到输入手机验证码界面</li>
<li>如果输入推行验证码不对，一直重复步骤2</li>
</ol>
<h2 id="问题："><a href="#问题：" class="headerlink" title="问题："></a>问题：</h2><h3 id="1-绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免"><a href="#1-绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免" class="headerlink" title="1. 绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免?"></a>1. 绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免?</h3><h3 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案:"></a>解决方案:</h3><ol>
<li>在 【手机号+业务】中增加一个图形验证码的key, 在校验之前，先校验用户当前提交的手机号和图形验证码是否匹配<ol>
<li>匹配：清空用户当前的图形验证码，需要重新请求图形验证码接口才能进行下一次请求</li>
<li>超时：图形验证码有效时间为60秒，超过60秒需要重新请求图形验证码，重新请求短信接口</li>
<li>不匹配，返回错误信息</li>
</ol>
</li>
<li>设置校验开关，如果需要在短信接口加入图形验证码的校验，则在发送短信之前，需要当前的图形验证码是否吻合（如果没有图形验证码说明没有经过图形验证这一步骤，直接返回失败）<ol>
<li>图形验证码校验关闭，则不会涉及用户图形验证码和手机号的匹配（考虑临时需要关闭这种校验的情况）</li>
<li>开启，则会进行上面所说的匹配操作</li>
</ol>
</li>
<li>图形验证码设置为通过校验之后失效，下次请求需要携带新的图形验证码，才能请求通过短信接口</li>
</ol>
<h3 id="2-增加【手机号-业务】的配置校验"><a href="#2-增加【手机号-业务】的配置校验" class="headerlink" title="2. 增加【手机号-业务】的配置校验"></a>2. 增加【手机号-业务】的配置校验</h3><p>解决方案：</p>
<ol>
<li>短信模块需要在js请求增加模块参数，如果没有模块参数，视为非法请求</li>
<li>请求带入 手机号-业务-key ，存储当前手机号对应业务的请求次数</li>
<li>增加判断<ol>
<li>如果请求次数在<strong>当天</strong>内超过xml配置次数，将不再允许改手机号对应该接口进行请求，不再发送短信</li>
<li>但是如果超过了一天之后再次请求，需要将请求次数 <strong>重置为1</strong>，也可以重新发送短信接口请求</li>
</ol>
</li>
<li>在需要的地方调用工具包即可</li>
</ol>
<h1 id="实现过程："><a href="#实现过程：" class="headerlink" title="实现过程："></a>实现过程：</h1><h2 id="成果："><a href="#成果：" class="headerlink" title="成果："></a>成果：</h2><h3 id="控制器调用："><a href="#控制器调用：" class="headerlink" title="控制器调用："></a>控制器调用：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// 开启之后，才做进一步校验</span></span><br><span class="line"><span class="keyword">if</span>(PHONE_MODULE_CHECK_ENABLE)&#123;</span><br><span class="line">    <span class="comment">// 添加 【手机+业务模块】校验 以及 【60秒重复调用校验】</span></span><br><span class="line">    <span class="keyword">boolean</span> checkRequest = CheckSendMailHelper.checkContextMap(result, request, phone);</span><br><span class="line">    <span class="comment">// 校验不通过的处理办法，可以自定</span></span><br><span class="line">    <span class="keyword">if</span> (!checkRequest) &#123;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//限制用户ip访问短信机获取验证码次数，默认10次</span></span><br><span class="line"><span class="keyword">if</span> (IP_CHECK_ENABLE &amp;&amp; !CheckIpVisitHelper.check(request)) &#123;</span><br><span class="line">    <span class="comment">// 校验不通过的处理办法，可以自定</span></span><br><span class="line">    result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_6.getCode());</span><br><span class="line">    result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_6.getName());</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>PHONE_MODULE_CHECK_ENABLE：短信-业务模块校验开关</p>
<p>IP_CHECK_ENABLE：限制短信每天的获取次数，也是手机号+业务模块</p>
</blockquote>
<h3 id="短信相关的枚举常量："><a href="#短信相关的枚举常量：" class="headerlink" title="短信相关的枚举常量："></a>短信相关的枚举常量：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> SmsRequestStatusEnum &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回状态码 表示发送正常</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_1(<span class="number">1</span>, <span class="string">"返回状态码 表示发送正常"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 60s内只能获取不能重复获取验证码</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_2(<span class="number">2</span>, <span class="string">"60s内只能获取不能重复获取验证码"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号码长度不正确</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_3(<span class="number">3</span>, <span class="string">"手机号码长度不正确"</span>),</span><br><span class="line"><span class="comment">//    /**</span></span><br><span class="line"><span class="comment">//     * 用户session已失效</span></span><br><span class="line"><span class="comment">//     */</span></span><br><span class="line"><span class="comment">//    RESULT_STATUS_4(4, "用户session已失效"),</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 缺少必要的参数：手机号！</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_4(<span class="number">4</span>, <span class="string">"缺少必要的参数：手机号！"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号码长度不正确</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_5(<span class="number">5</span>, <span class="string">"手机号码长度不正确"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 同一个ip请求短信机次数过于频繁</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_6(<span class="number">6</span>, <span class="string">"同一个ip请求短信机次数过于频繁！"</span>),</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 60秒内不允许重复请求短信接口</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_7(<span class="number">7</span>, <span class="string">"60秒内不允许重复请求短信接口！"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 缺少必要的请求参数：短信业务模块名称：phoneModule ！</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_9(<span class="number">9</span>, <span class="string">"缺少必要的请求参数：短信业务模块名称：phoneModule ！"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 当前手机号请求超出限制，请等待24小时之后重新请求短信接口</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_10(<span class="number">10</span>, <span class="string">"当前手机号请求次数超出限制，请等待24小时之后重新请求短信接口"</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 图形验证码已失效，请重新请求短信接口！</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RESULT_STATUS_8(<span class="number">8</span>, <span class="string">"图形验证码已失效，请重新请求短信接口！"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> code;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    SmsRequestStatusEnum(<span class="keyword">int</span> code, String name) &#123;</span><br><span class="line">        <span class="keyword">this</span>.code = code;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> code;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getName</span><span class="params">(<span class="keyword">int</span> code)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (SmsRequestStatusEnum item : SmsRequestStatusEnum.values()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (item.getCode() == code) &#123;</span><br><span class="line">                <span class="keyword">return</span> item.getName();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">""</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="IP检测工具类："><a href="#IP检测工具类：" class="headerlink" title="IP检测工具类："></a>IP检测工具类：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 检测ip访问辅助类，</span></span><br><span class="line"><span class="comment"> * 主要处理某个时间段类，</span></span><br><span class="line"><span class="comment"> * ip访问次数，以及设置封禁时间、</span></span><br><span class="line"><span class="comment"> * 解封等操作，</span></span><br><span class="line"><span class="comment"> * 用于防止频繁调用短信机攻击等</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 重写原理：</span></span><br><span class="line"><span class="comment"> * 1. 使用LRUMap key 存储 IP号码，value 存储 访问次数以及时间（使用map）</span></span><br><span class="line"><span class="comment"> * 2. 使用servletContext 存储 LRUMap，LRUMap 存储 的 key 为 IP号码-业务模块 VALUE 为 map</span></span><br><span class="line"><span class="comment"> * 3. LRUMap 对应的 key IP号码+业务。 value 绑定了访问次数和时间</span></span><br><span class="line"><span class="comment"> * 4. 如果没有配置模块，校验将会永久失败，IP的模块和短信的模块使用同一块配置</span></span><br><span class="line"><span class="comment"> * 5. ServletContext 生命周期和web的生命周期相同</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 2020/09/08 重写工具类，</span></span><br><span class="line"><span class="comment"> * 1. 不在暴露 map。</span></span><br><span class="line"><span class="comment"> * 2. 使用servletContext 保存 Ip 的 map。Map&lt;String,Object&gt; 形式</span></span><br><span class="line"><span class="comment"> * 3. 如果超过IP限制时间，自动进行解锁</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> xd</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CheckIpVisitHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日志使用 短信的key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(<span class="string">"phoneCode"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机访问限制初始化的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> PHONE_REQUEST_INIT_SIZE = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 封禁的时间（单位毫秒）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> FORBIDEN_TIME = <span class="number">60</span> * <span class="number">1000</span> * <span class="number">60</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 超过访问时间重新计时（单位毫秒）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MININTEVAL = <span class="number">60</span> * <span class="number">1000</span> * <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * LRU Map 初始化大小</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> LRU_MAP_INIT_SIZE = <span class="number">100000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * IP 在指定时间内的限制次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> IP_MAX_VISIST_TIME = Setter.getInt(<span class="string">"sms.ip-size"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ip检测使用的 Map key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String IP_CHECK_MAP = <span class="string">"IP_CHECK_MAP"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 请求次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String VISIT_COUNT_KEY = <span class="string">"visit_count"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 最后的请求时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String VISIT_TIME_KEY = <span class="string">"visit_time"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * IP号码-业务模块名称的格式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String IP_MOUDULE_FORMAT = <span class="string">"%s-%s"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ip检查工具，将map 放入 ServletContext</span></span><br><span class="line"><span class="comment">     * 1. 检测基于 ServletContext</span></span><br><span class="line"><span class="comment">     * 2. 请附带 phoneModule: 否则校验永远为false</span></span><br><span class="line"><span class="comment">     * map 当中:</span></span><br><span class="line"><span class="comment">     * key: IP号码-业务</span></span><br><span class="line"><span class="comment">     * value:</span></span><br><span class="line"><span class="comment">     * map -&gt; &#123;</span></span><br><span class="line"><span class="comment">     *  key: 请求次数：value: int</span></span><br><span class="line"><span class="comment">     *  key：请求的时间：value：date</span></span><br><span class="line"><span class="comment">     * &#125;</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request request请求域</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 如果校验没有超过限制 返回 true ，否则返回false</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">check</span><span class="params">(HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        String remoteIp = RequestHelper.getRemoteIp(request);</span><br><span class="line">        ServletContext servletContext = request.getServletContext();</span><br><span class="line">        LRUMap attribute = (LRUMap) servletContext.getAttribute(IP_CHECK_MAP);</span><br><span class="line">        <span class="keyword">if</span> (Objects.isNull(attribute)) &#123;</span><br><span class="line">            attribute = <span class="keyword">new</span> LRUMap(LRU_MAP_INIT_SIZE);</span><br><span class="line">            servletContext.setAttribute(IP_CHECK_MAP, attribute);</span><br><span class="line">        &#125;</span><br><span class="line">        Date now = <span class="keyword">new</span> Date();</span><br><span class="line">        <span class="comment">// 根据 IP + 业务模块进行绑定</span></span><br><span class="line">        <span class="comment">// 获取请求的模块名称 同时检查是否有配置模块</span></span><br><span class="line">        String phoneMouduleFlag = CheckSendMailHelper.checkExistsAndGetModule(request);</span><br><span class="line">        <span class="keyword">if</span> (phoneMouduleFlag == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// IP号码 -业务名称</span></span><br><span class="line">        String modulePhone = String.format(IP_MOUDULE_FORMAT, remoteIp, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 获取ip对应的的当前请求次数和请求时间</span></span><br><span class="line">        Map&lt;String, Object&gt; ipMap = (Map&lt;String, Object&gt;) attribute.get(modulePhone);</span><br><span class="line">        <span class="comment">// 如果当前ip没有访问过</span></span><br><span class="line">        <span class="keyword">if</span> (MapUtils.isEmpty(ipMap)) &#123;</span><br><span class="line">            ipMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">            ipMap.put(VISIT_COUNT_KEY, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">            ipMap.put(VISIT_TIME_KEY, now);</span><br><span class="line">            attribute.putIfAbsent(modulePhone, ipMap);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> visitCount = (<span class="keyword">int</span>) ipMap.get(VISIT_COUNT_KEY);</span><br><span class="line">        Date visitDate = (Date) ipMap.get(VISIT_TIME_KEY);</span><br><span class="line">        <span class="comment">// 如果长时间没有访问，重新计算</span></span><br><span class="line">        <span class="keyword">if</span> (now.getTime() - visitDate.getTime() &gt; MININTEVAL) &#123;</span><br><span class="line">            ipMap.put(VISIT_COUNT_KEY, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">            ipMap.put(VISIT_TIME_KEY, now);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果访问的次数超过了限制的次数</span></span><br><span class="line">        <span class="keyword">if</span> (visitCount &gt; IP_MAX_VISIST_TIME) &#123;</span><br><span class="line">            <span class="comment">// 如果已经到达限制的次数，但是访问时间超过了限制的时间，重新计时，重新计算请求次数</span></span><br><span class="line">            <span class="keyword">if</span> (now.getTime() - visitDate.getTime() &gt; FORBIDEN_TIME) &#123;</span><br><span class="line">                ipMap.put(VISIT_COUNT_KEY, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">                ipMap.put(VISIT_TIME_KEY, now);</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            logger.info(<span class="string">"当前IP: &#123;&#125; 请求次数超过限制"</span>, remoteIp);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// IP访问次数 + 1</span></span><br><span class="line">            visitCount++;</span><br><span class="line">            <span class="comment">// 更新访问次数</span></span><br><span class="line">            ipMap.put(VISIT_COUNT_KEY, visitCount);</span><br><span class="line">            <span class="comment">// 更新访问时间</span></span><br><span class="line">            ipMap.put(VISIT_TIME_KEY, now);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<ol>
<li>使用的是servlet-context 全局变量作为存储，依赖web的服务器空间大小，当短信号码过量会造成服务器可访问内存不够，可以考虑用redis 等中间件去存储</li>
<li>LRUMap：使用最少使用内容作为缓存的设计，存储业务需要判断的手机号等</li>
<li>静态方法意味着会出现并发的问题，整个工具类是线程不安全的。</li>
</ol>
</blockquote>
<h3 id="短信发送校验工具类："><a href="#短信发送校验工具类：" class="headerlink" title="短信发送校验工具类："></a>短信发送校验工具类：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 短信发送校验工具类</span></span><br><span class="line"><span class="comment"> * map 存储的 key 为手机号码-业务</span></span><br><span class="line"><span class="comment"> * value  为 发送对象等其他信息</span></span><br><span class="line"><span class="comment"> * 包含</span></span><br><span class="line"><span class="comment"> * 1. 图形验证码（不开放不做校验）</span></span><br><span class="line"><span class="comment"> * 2. 图形验证码有效时间</span></span><br><span class="line"><span class="comment"> * 3. 【手机号-业务】 key-name 的配置</span></span><br><span class="line"><span class="comment"> * 4. 【手机号-业务-锁定时间】 key-date</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> lazytimes</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2020/09/02 10:21</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CheckSendMailHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 短信验证码配置</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(<span class="string">"phoneCode"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 60 秒内不允许重复请求</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> PHONE_REQUEST_TIME = <span class="number">60</span> * <span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 60 秒 内 图形验证码有效</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> CAPTCHA_REQUEST_TIME = <span class="number">60</span> * <span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 用户模块手机号的限制时间 24 小时</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> PHONE_REQUEST_WAIT_TIME = <span class="number">60</span> * <span class="number">1000</span> * <span class="number">24</span> * <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机访问限制初始化的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> PHONE_REQUEST_INIT_SIZE = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 请求上下文的map key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CONTEXT_MAP = <span class="string">"CONTEXT_MAP"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号-业务模块名称的格式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String PHONE_MOUDULE_FORMAT = <span class="string">"%s-%s"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号-业务模块-请求key 的格式标注用户当前模块的请求 定时器</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String PHONE_MOUDULE_TIMER_FORMAT = <span class="string">"%s-%s-timer"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 短信验证码模块的通用格式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String SMS_MODULE_TEMPLATE = <span class="string">"sms.modules.%s"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号-业务-图形验证码 模块名称的格式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CAPTCHA_MOUDULE_FORMAT = <span class="string">"%s-%s-captcha"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号-业务模块-图形验证码-请求key 的格式标注用户当前模块的请求 图形验证码 每个手机号对应业务一份</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CAPTCHA_MOUDULE_TIMER_FORMAT = <span class="string">"%s-%s-captcha-timer"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 业务模块名称参数Key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String PHONE_MOUDULE_FLAG = <span class="string">"phoneModule"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 图形验证码key</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CAPCHACODE = <span class="string">"capchaCode"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    /**</span></span><br><span class="line"><span class="comment">//     * 最后发送时间key</span></span><br><span class="line"><span class="comment">//     */</span></span><br><span class="line"><span class="comment">//    private static final String LAST_SEND_TIME = "lastSendTime";</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 图形验证码开关</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">boolean</span> CAPTCHA_ENABLE = Setter.getBoolean(<span class="string">"captcha.enable"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 为当前的用户手机号码绑定 图形验证码</span></span><br><span class="line"><span class="comment">     * 图形验证码用于短信接口请求使用，超过一定时间，图形验证码失效</span></span><br><span class="line"><span class="comment">     * 【手机号-业务-图形验证码】：key</span></span><br><span class="line"><span class="comment">     * 【手机号-业务-图形验证码-超时时间】:key</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> phoneCode 手机号</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> code      图形验证码</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request   请求</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">addCapcha</span><span class="params">(String phoneCode, String code, HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!CAPTCHA_ENABLE) &#123;</span><br><span class="line">            logger.info(<span class="string">"请开启图形验证码校验之后，再配合本工具类方法使用！"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ServletContext servletContext = request.getServletContext();</span><br><span class="line">        Map&lt;String, Map&lt;String, Object&gt;&gt; attribute = initServletContextMap(servletContext);</span><br><span class="line">        Date now = <span class="keyword">new</span> Date();</span><br><span class="line">        <span class="comment">// 获取请求的模块名称 同时检查是否有配置模块</span></span><br><span class="line">        String phoneMouduleFlag = checkExistsAndGetModule(request);</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isBlank(phoneMouduleFlag)) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 手机号 -业务名称</span></span><br><span class="line">        String modulePhone = String.format(PHONE_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 手机号- 业务名称 - 图形验证码</span></span><br><span class="line">        String capchaModule = String.format(CAPTCHA_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 手机号 - 业务名称 -图形验证码 - 定时</span></span><br><span class="line">        String capchaModuleTimer = String.format(CAPTCHA_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="keyword">if</span> (!attribute.containsKey(modulePhone)) &#123;</span><br><span class="line">            HashMap&lt;String, Object&gt; stringObjectHashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">            stringObjectHashMap.put(capchaModule, code);</span><br><span class="line">            <span class="comment">// 图片的有效期</span></span><br><span class="line">            stringObjectHashMap.put(capchaModuleTimer, now);</span><br><span class="line">            attribute.put(modulePhone, stringObjectHashMap);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            Map&lt;String, Object&gt; stringObjectMap = attribute.get(modulePhone);</span><br><span class="line">            <span class="comment">// 更新验证码以及有效期</span></span><br><span class="line">            stringObjectMap.put(capchaModule, code);</span><br><span class="line">            <span class="comment">// 图片的有效期</span></span><br><span class="line">            stringObjectMap.put(capchaModuleTimer, now);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机号限制发送处理</span></span><br><span class="line"><span class="comment">     * 1. 增加对于用户请求短信接口的限制，60秒访问一次</span></span><br><span class="line"><span class="comment">     * 2. 增加图形验证码和用户的手机号绑定匹配</span></span><br><span class="line"><span class="comment">     * 1. 图形校验可以灵活开放和关闭</span></span><br><span class="line"><span class="comment">     * 3. 【手机号-业务】的key配置，短信接口当中需要对于用户的请求做限制</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> result    封装了返回的状态和信息的 result</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request   请求request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> phoneCode 手机号码</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">checkContextMap</span><span class="params">(Map&lt;String, Object&gt; result, HttpServletRequest request, String phoneCode)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 获取当前模块配置Map集合</span></span><br><span class="line">        ServletContext servletContext = request.getServletContext();</span><br><span class="line">        Map&lt;String, Map&lt;String, Object&gt;&gt; attribute = initServletContextMap(servletContext);</span><br><span class="line">        Date now = <span class="keyword">new</span> Date();</span><br><span class="line">        <span class="comment">// 获取请求的模块名称</span></span><br><span class="line">        String phoneMouduleFlag = checkExistsAndGetModule(request);</span><br><span class="line">        <span class="keyword">if</span> (phoneMouduleFlag == <span class="keyword">null</span>) &#123;</span><br><span class="line">            result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_9.getCode());</span><br><span class="line">            result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_9.getName());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//  当前短信业务模块【手机号-业务】</span></span><br><span class="line">        String modulePhone = String.format(PHONE_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 当前模块【手机号-业务-请求限制时间】</span></span><br><span class="line">        String modulePhoneTimer = String.format(PHONE_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 当前模块每个用户每天最多请求次数</span></span><br><span class="line">        <span class="keyword">int</span> moduleCount = Setter.getInt(String.format(SMS_MODULE_TEMPLATE, phoneMouduleFlag));</span><br><span class="line">        <span class="keyword">if</span> (!attribute.containsKey(modulePhone)) &#123;</span><br><span class="line">            <span class="comment">// 需要自行初始化</span></span><br><span class="line">            HashMap&lt;String, Object&gt; stringObjectHashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">            <span class="comment">// 初始化短信接口调用次数</span></span><br><span class="line">            stringObjectHashMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">            <span class="comment">// 初始化短信接口调用时间</span></span><br><span class="line">            stringObjectHashMap.put(modulePhoneTimer, now);</span><br><span class="line">            attribute.put(modulePhone, stringObjectHashMap);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            Map&lt;String, Object&gt; objectMap = attribute.get(modulePhone);</span><br><span class="line">            <span class="comment">// 开启图形验证码校验才做处理</span></span><br><span class="line">            <span class="keyword">if</span> (CAPTCHA_ENABLE) &#123;</span><br><span class="line">                <span class="keyword">if</span> (!checkCatpchaCode(result, request, phoneCode, now, phoneMouduleFlag, objectMap)) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 获取当前【手机号+业务】的对应 访问次数，以及最后的访问时间</span></span><br><span class="line">            Object count = objectMap.get(modulePhone);</span><br><span class="line">            Object timer = objectMap.get(modulePhoneTimer);</span><br><span class="line">            <span class="comment">// 初始化</span></span><br><span class="line">            <span class="keyword">if</span> (Objects.isNull(count) || Objects.isNull(timer)) &#123;</span><br><span class="line">                objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">                objectMap.put(modulePhoneTimer, now);</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            Integer integer = Integer.valueOf(objectMap.get(modulePhone).toString());</span><br><span class="line">            Date time = (Date) timer;</span><br><span class="line">            <span class="comment">// 检查当前短信+业务是否在60秒内访问</span></span><br><span class="line">            <span class="keyword">if</span>(!checkLastGetTime(result, now, time))&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//如果长时间未访问，重置</span></span><br><span class="line">            <span class="keyword">if</span> ((now.getTime() - time.getTime()) &gt; PHONE_REQUEST_WAIT_TIME) &#123;</span><br><span class="line">                <span class="comment">//  刷新时间</span></span><br><span class="line">                objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">                objectMap.put(modulePhoneTimer, now);</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//   当前模块超过了请求限制</span></span><br><span class="line">            <span class="keyword">if</span> (integer &gt; moduleCount) &#123;</span><br><span class="line">                <span class="comment">// 超过了请求时间限制，解封</span></span><br><span class="line">                <span class="keyword">if</span> (now.getTime() - time.getTime() &gt; PHONE_REQUEST_WAIT_TIME) &#123;</span><br><span class="line">                    <span class="comment">//  刷新时间</span></span><br><span class="line">                    objectMap.put(modulePhone, PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">                    objectMap.put(modulePhoneTimer, now);</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_10.getCode());</span><br><span class="line">                result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_10.getName());</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 模块请求次数 + 1</span></span><br><span class="line">            objectMap.put(modulePhone, integer + PHONE_REQUEST_INIT_SIZE);</span><br><span class="line">            <span class="comment">// 刷新时间</span></span><br><span class="line">            objectMap.put(modulePhoneTimer, now);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 校验图形验证码</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> result           返回处理结果</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request          请求</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> phoneCode        手机号</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> now              当前时间</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> phoneMouduleFlag 手机号 - 业务模块 标识</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> objectMap        servletContext 对象</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">checkCatpchaCode</span><span class="params">(Map&lt;String, Object&gt; result, HttpServletRequest request, String phoneCode, Date now, String phoneMouduleFlag, Map&lt;String, Object&gt; objectMap)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 手机号- 业务名称 - 图形验证码</span></span><br><span class="line">        String capchaModule = String.format(CAPTCHA_MOUDULE_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 手机号 - 业务名称 -图形验证码 - 定时</span></span><br><span class="line">        String capchaModuleTimer = String.format(CAPTCHA_MOUDULE_TIMER_FORMAT, phoneCode, phoneMouduleFlag);</span><br><span class="line">        <span class="comment">// 图形验证码超过60秒失效</span></span><br><span class="line">        Date captchaCodeValidPeriod = (Date) objectMap.get(capchaModuleTimer);</span><br><span class="line">        <span class="comment">// 获取请求参数的验证码</span></span><br><span class="line">        String requestCaptchaCode = RequestHelper.getString(CAPCHACODE, request);</span><br><span class="line">        <span class="comment">// 拿到map中的图形验证码</span></span><br><span class="line">        Object requestCode = objectMap.get(capchaModule);</span><br><span class="line">        <span class="comment">// 是否存在图形验证码的参数，同时比对是否和请求参数一致</span></span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isBlank(requestCaptchaCode) || Objects.isNull(requestCode)) &#123;</span><br><span class="line">            result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_8.getCode());</span><br><span class="line">            result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_8.getName());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果超时或者图形验证码不匹配，需要重新请求图形验证码</span></span><br><span class="line">        <span class="keyword">if</span> (!Objects.equals(requestCaptchaCode, requestCode.toString()) || (now.getTime() - captchaCodeValidPeriod.getTime() &gt; (CAPTCHA_REQUEST_TIME))) &#123;</span><br><span class="line">            result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_8.getCode());</span><br><span class="line">            result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_8.getName());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 清空用户的图形验证码</span></span><br><span class="line">            objectMap.put(capchaModule, <span class="keyword">null</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 检查最后的访问时间是否在指定时间内容</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> result    返回对象结果</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> now       当前时间</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">checkLastGetTime</span><span class="params">(Map&lt;String, Object&gt; result, Date now, Date lastSend)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 60 秒内不允许再次发送</span></span><br><span class="line">        <span class="keyword">if</span> ((now.getTime() - lastSend.getTime()) &lt;= PHONE_REQUEST_TIME) &#123;</span><br><span class="line">            result.put(<span class="string">"result"</span>, SmsRequestStatusEnum.RESULT_STATUS_7.getCode());</span><br><span class="line">            result.put(<span class="string">"msg"</span>, SmsRequestStatusEnum.RESULT_STATUS_7.getName());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化全局上下文的Map容器</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> servletContext 上下文</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 初始化之后的map参数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Map&lt;String, Map&lt;String, Object&gt;&gt; initServletContextMap(ServletContext servletContext) &#123;</span><br><span class="line">        Map&lt;String, Map&lt;String, Object&gt;&gt; attribute = (Map&lt;String, Map&lt;String, Object&gt;&gt;) servletContext.getAttribute(CONTEXT_MAP);</span><br><span class="line">        <span class="keyword">if</span> (Objects.isNull(attribute)) &#123;</span><br><span class="line">            attribute = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">            servletContext.setAttribute(CONTEXT_MAP, attribute);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> attribute;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 检查请求参数中是否存在业务模块配置</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request 请求request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> String <span class="title">checkExistsAndGetModule</span><span class="params">(HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        String phoneMouduleFlag = RequestHelper.getString(PHONE_MOUDULE_FLAG, request);</span><br><span class="line">        String moduleNo = Setter.getString(String.format(SMS_MODULE_TEMPLATE, phoneMouduleFlag));</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isBlank(moduleNo)) &#123;</span><br><span class="line">            logger.info(<span class="string">"未找到对应的短信模块，请在xml配置短信模块名称，并在请求参数中加入 phoneModule: 对应模块名称之后再进行请求"</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> phoneMouduleFlag;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<ol>
<li>使用手机号-业务模块。先获取是否存在对应的模块，然后进行校验</li>
<li>图形验证码的方法需要开启推行验证码的情况下，配合使用</li>
<li>HashMap的线程是不安全的，可以考虑使用ConcurrentHashMap</li>
</ol>
</blockquote>
<h3 id="图形验证码的配置："><a href="#图形验证码的配置：" class="headerlink" title="图形验证码的配置："></a>图形验证码的配置：</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- =================================================================== --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 核心：图形验证码的通用配置 --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- =================================================================== --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">captcha</span> <span class="attr">description</span>=<span class="string">"图形验证码的通用配置"</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">enable</span> <span class="attr">description</span>=<span class="string">"是否开放图形验证码"</span> <span class="attr">value</span>=<span class="string">"false"</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">length</span> <span class="attr">description</span>=<span class="string">"设置字符长度"</span> <span class="attr">value</span>=<span class="string">"5"</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 验证码图片的宽度 默认 160 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">width</span> <span class="attr">description</span>=<span class="string">"设置动画宽度"</span> <span class="attr">value</span>=<span class="string">"160"</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 验证码图片的高度 默认 50 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">height</span> <span class="attr">description</span>=<span class="string">"设置动画宽度"</span> <span class="attr">value</span>=<span class="string">"50"</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">captcha</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="短信验证码的配置："><a href="#短信验证码的配置：" class="headerlink" title="短信验证码的配置："></a>短信验证码的配置：</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- =================================================================== --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--系统发送短信配置 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- =================================================================== --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sms</span> <span class="attr">description</span>=<span class="string">"webService短信机服务配置"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">isopen</span> <span class="attr">description</span>=<span class="string">"是否开启短信发送"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 模块配置： 需要 name 模块名称，用于短信校验 和 value 表示每天最多的请求次数 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">modules</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 注册模块 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">registered</span> <span class="attr">description</span>=<span class="string">"注册模块"</span> <span class="attr">value</span>=<span class="string">"5"</span>/&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 信箱请求短信验证码 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">mailbox</span> <span class="attr">description</span>=<span class="string">"信箱模块"</span> <span class="attr">value</span>=<span class="string">"10"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">modules</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">ip-size</span> <span class="attr">description</span>=<span class="string">"ip检测的限制次数"</span> <span class="attr">value</span>=<span class="string">"10"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">phoneMoudleCheck-enable</span> <span class="attr">description</span>=<span class="string">"手机号-业务模块校验是否开启"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">ip-enable</span> <span class="attr">description</span>=<span class="string">"IP检测开关"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">sms</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h1 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h1><ol>
<li>工具类基于配置进行开关配置</li>
<li>按照手机号+业务模块，划分同一手机号在不同的业务模块进行校验拦截</li>
<li>图形验证码可以配合短信接口使用，但是目前来看耦合还是有点严重</li>
</ol>
<h1 id="结语："><a href="#结语：" class="headerlink" title="结语："></a>结语：</h1><p>本人学艺不精，代码写的比较烂，这篇文章算是给自己留坑以后填。</p>
<p>如果看文章费劲头，专门另写一篇说说独立使用。</p>
<p>小小工具类，仅供参考</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2020/10/29/mail/" title="一次项目短信验证码整改实验">https://whitestore.top/2020/10/29/mail/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"># Java</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/10/27/code/" rel="prev" title="雪花算法代码">
      <i class="fa fa-chevron-left"></i> 雪花算法代码
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/11/03/neo4j1/" rel="next" title="我的Neo4j探索之旅 - 初识Neo4j（一）">
      我的Neo4j探索之旅 - 初识Neo4j（一） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一次短信验证码整改实验"><span class="nav-number">1.</span> <span class="nav-text">一次短信验证码整改实验</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言："><span class="nav-number">1.1.</span> <span class="nav-text">前言：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#需求"><span class="nav-number">1.2.</span> <span class="nav-text">需求</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#讨论内容"><span class="nav-number">2.</span> <span class="nav-text">讨论内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#处理方案："><span class="nav-number">2.1.</span> <span class="nav-text">处理方案：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第三方图形验证码参考"><span class="nav-number">2.2.</span> <span class="nav-text">第三方图形验证码参考</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#图形验证码："><span class="nav-number">2.2.1.</span> <span class="nav-text">图形验证码：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#短信校验"><span class="nav-number">2.2.2.</span> <span class="nav-text">短信校验</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现"><span class="nav-number">2.3.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#大致流程"><span class="nav-number">2.3.1.</span> <span class="nav-text">大致流程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#问题："><span class="nav-number">2.4.</span> <span class="nav-text">问题：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免"><span class="nav-number">2.4.1.</span> <span class="nav-text">1. 绕过图形验证码的接口，直接访问短信接口进行攻击，如何避免?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方案"><span class="nav-number">2.4.2.</span> <span class="nav-text">解决方案:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-增加【手机号-业务】的配置校验"><span class="nav-number">2.4.3.</span> <span class="nav-text">2. 增加【手机号-业务】的配置校验</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实现过程："><span class="nav-number">3.</span> <span class="nav-text">实现过程：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#成果："><span class="nav-number">3.1.</span> <span class="nav-text">成果：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#控制器调用："><span class="nav-number">3.1.1.</span> <span class="nav-text">控制器调用：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#短信相关的枚举常量："><span class="nav-number">3.1.2.</span> <span class="nav-text">短信相关的枚举常量：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#IP检测工具类："><span class="nav-number">3.1.3.</span> <span class="nav-text">IP检测工具类：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#短信发送校验工具类："><span class="nav-number">3.1.4.</span> <span class="nav-text">短信发送校验工具类：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图形验证码的配置："><span class="nav-number">3.1.5.</span> <span class="nav-text">图形验证码的配置：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#短信验证码的配置："><span class="nav-number">3.1.6.</span> <span class="nav-text">短信验证码的配置：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结："><span class="nav-number">4.</span> <span class="nav-text">总结：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#结语："><span class="nav-number">5.</span> <span class="nav-text">结语：</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
